/*
 * @(#)NameConsistencyChecker.java   09/10/12
 * 
 * Copyright (C) 2009 Ecole des Mines d'Ales
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Contact: yulinz88@gmail.com
 */



package fr.ema.arch3d.analyser.consistencyChecker.consistencyCheckerImpl;

import java.util.ArrayList;
import java.util.List;

import fr.ema.arch3d.analyser.consistencyChecker.consistencyCheckerItf.NameConsistencyChecker;
import fr.ema.arch3d.jmodel.assembly.Assembly;
import fr.ema.arch3d.jmodel.configuration.Configuration;
import fr.ema.arch3d.jmodel.specification.Specification;
import fr.ema.arch3d.xoadl.assembly.AssemblyConstraintADL;
import fr.ema.arch3d.xoadl.assembly.ComponentInstanceADL;
import fr.ema.arch3d.xoadl.assembly.ConnectorInstanceADL;
import fr.ema.arch3d.xoadl.configuration.ComponentClassADL;
import fr.ema.arch3d.xoadl.specification.ComponentRoleADL;
import fr.ema.arch3d.xoadl.specification.ConnectionADL;


//~--- classes ----------------------------------------------------------------

/**
 * Class description
 *
 *
 * @version 1.0, 09/09/18
 * @author Huaxi (Yulin) ZHANG
 */
public class NameConsistencyCheckerImpl implements NameConsistencyChecker{

    /**
     * Check the new added component role whether has a same name with one of the
     * component role in the existing specification.
     *
     * @param spec  the existing specification that this component role wants to add.
     * @param cmpRole  the new component role should be checked.
     */
    public void check(Specification spec, ComponentRoleADL cmpRole) {
        for (int i = 0; i < spec.getComponentRole().size(); i++) {
            if (cmpRole.getName().equals(((ComponentRoleADL)spec.getComponentRole().get(i)).getName())) {
                System.err.println("The component role \'" + cmpRole.getName()
                                   + "\' with the same name has already existed in specification " + spec.getName());
            }
        }
    }
//    /**
//     * Check the new added component role whether has a same name with one of the
//     * component role in the existing specification.
//     *
//     * @param spec  the existing specification that this component role wants to add.
//     * @param cmpRole  the new component role should be checked.
//     */
//    public static void check(Configuration config, ComponentClassADL cmpClass) {
//	List cmpList=new ArrayList();
//	if(config.getCompositeComponentClass()!=null)
//	    cmpList.addAll(config.getCompositeComponentClass());
//	if(config.getPrimitiveComponentClass()!=null)
//	    cmpList.addAll(config.getPrimitiveComponentClass());
//        for (int i = 0; i < cmpList.size(); i++) {
//            if (cmpClass.getName().equals(((ComponentClassADL)cmpList.get(i)).getName())) {
//                System.err.println("The component class \'" + cmpRole.getName()
//                                   + "\' with the same name has already existed in " + spec.getName());
//            }
//        }
//    }
    
    /**
     * Check the new added connection whether has a same name with one of the
     * connection in the existing specification.
     *
     * @param spec  the existing specification that this connection wants to add.
     * @param connection  the new connection should be checked.
     */
    public void check(Specification spec, ConnectionADL connection) {
        for (int i = 0; i < spec.getConnection().size(); i++) {
            if (connection.getName().equals(((ConnectionADL)spec.getConnection().get(i)).getName())) {
                System.err.println("The connection \'" + connection.getName()
                                   + "\' with the same name has already existed in specification " + spec.getName());
            }
        }
    }
    
    /**
     * Check the new added component instance whether has a same name with one of the
     * component instance in the existing assembly.
     *
     * @param ass  the existing assembly that this component instance wants to add.
     * @param cmpIns  the new component instance should be checked.
     */
    public void check(Assembly ass, ComponentInstanceADL cmpIns) {
        for (int i = 0; i < ass.getComponentInstance().size(); i++) {
            if (cmpIns.getName().equals(((ComponentInstanceADL)ass.getComponentInstance().get(i)).getName())) {
                System.err.println("The component instance \'" + cmpIns.getName()
                                   + "\' with the same name has already existed in assembly " + ass.getName());
            }
        }
    }
    
    /**
     * Check the new added connector instance whether has a same name with one of the
     * connector instance in the existing assembly.
     *
     * @param ass  the existing assembly that this connector instance wants to add.
     * @param ctrIns  the new connector instance should be checked.
     */
    public void check(Assembly ass, ConnectorInstanceADL ctrIns) {
        for (int i = 0; i < ass.getComponentInstance().size(); i++) {
            if (ctrIns.getName().equals(((ComponentInstanceADL)ass.getComponentInstance().get(i)).getName())) {
                System.err.println("The connector instance \'" + ctrIns.getName()
                                   + "\' with the same name has already existed in assembly " + ass.getName());
            }
        }
    }
    
    /**
     * Check the new added assembly constraint whether has a same name with one of the
     * assembly constraint in the existing assembly.
     *
     * @param ass  the existing assembly that this assembly constraint wants to add.
     * @param assConstraint  the new assembly constraint should be checked.
     */
    public void check(Assembly ass, AssemblyConstraintADL assConstraint) {
        for (int i = 0; i < ass.getComponentInstance().size(); i++) {
            if (assConstraint.getName().equals(((ComponentInstanceADL)ass.getComponentInstance().get(i)).getName())) {
                System.err.println("The connector instance \'" + assConstraint.getName()
                                   + "\' with the same name has already existed in assembly " + ass.getName());
            }
        }
    }
}
